package Perlescent::Theme;

use Exporter qw(import);
use strict;
use warnings;
our(@ISA,@EXPORT,@EXPORT_OK);
	
@EXPORT = qw(
	     );
@EXPORT_OK = qw(
	        );

#use base qw( Perlescent );
@ISA = qw( Perlescent );

sub new
{
    my $class = shift;
    my $self = {};
    bless $self, $class;
    my $caller = caller;
    return $self;
}

sub readTemplate
{
	my $cfg = Perlescent->become('Config');
	shift; #if ref($_[0]);
	my($tmpl,$h,$f) = @_;

	my %config = $cfg->loadConfig;
	my $theme = $config{THEME};
	my $path = $config{FULL_PATH};
	my $html;
	my @tmpl;

	  for(my$i=0;$i<5;$i++)
	  {
	open(TMPL, "<", "$path/themes/$theme/$tmpl")||redo;
		@tmpl = <TMPL>;
	close TMPL;
		last;
	  }

	if(grep { /\$HEADER\$/ } @tmpl)
	{

	  for(my$i=0;$i<5;$i++)
	  {
		open(HDR, "<", "$path/themes/$theme/header") || redo;
			my@hdr = <HDR>;
		close HDR;

			foreach my $hdr (@hdr)
			{
				$html .= "$hdr\n";
			}

	  	last;
	  }

	}
		foreach my $t (@tmpl)
		{
			$html .= "$t\n";
		}

	if(grep { /\$FOOTER\$/ } @tmpl)
	{
		open(FTR, "<", "$path/themes/$theme/footer");
			my@ftr = <FTR>;
		close FTR;

			foreach my $ftr (@ftr)
			{
				$html .= "$ftr\n";
			}

		$html =~ s/\$HEADER\$//g;
		$html =~ s/\$FOOTER\$//g;
	}

	if(grep { /\$MENU\$/ } @tmpl)
	{
		open(MNU, "<", "$path/themes/$theme/menu");
			my@mnu = <MNU>;
		close MNU;
		my$menu;

			foreach my $mnu (@mnu)
			{
				$menu .= "$mnu\n";
			}

		$html =~ s/\$MENU\$/$menu/g;
	}

	foreach my $k (keys %config)
	{
		next unless ($k =~ /^P_/);
		next unless ($html =~ /$k/);
		my$v = $config{$k};
		$html =~ s/\$$k\$/$v/g;
	}

	return $html;
}

return 1;
